package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.mystatic.publicUrl;
import longcaisuyun.longcai.com.suyunbean.CityBean;



public class CityAcitvity extends Activity {

    ListView listView;
    List<CityBean> list = new ArrayList<CityBean>();
    Intent intent;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_acitvity);

        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        intent = getIntent();
        ViewInit();
        httpget();
    }

    private void ViewInit() {
        LinearLayout fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        listView = (ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intent.putExtra("id", list.get(position).getAreaid());
                intent.putExtra("content", list.get(position).getClassname());
                System.out.println("************" + list.get(position).getClassname());
                CityAcitvity.this.setResult(1, intent);
                CityAcitvity.this.finish();
            }
        });

    }

    public void httpget() {
        final FinalHttp finalHttp = new FinalHttp();
        finalHttp.get(publicUrl.city, new AjaxCallBack<String>() {

            public void onStart() {
                super.onStart();
//                MyApplication.myviewutil.startProgressDialog(CityAcitvity.this, finalHttp, publicUrl.city);

            }

            public void onSuccess(String s) {
                super.onSuccess(s);
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    String message = jsonObject.getString("message");
                    JSONArray jsonArray = jsonObject.getJSONArray("list");
                    tolist(jsonArray);
                    listView.setAdapter(new CityAdapter(CityAcitvity.this, list));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                MyApplication.myviewutil.stopProgressDialog();
            }

            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                MyApplication.myviewutil.stopProgressDialog();
                Toast.makeText(CityAcitvity.this, "网络故障,请稍后重试", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void tolist(JSONArray jsonArray) {
        for (int j = 0; j < jsonArray.length(); j++) {
            try {
                JSONObject jsonObject = (JSONObject) jsonArray.get(j);
                CityBean cityBean = new CityBean();

                cityBean.setAreaid(jsonObject.getString("areaid"));
                cityBean.setClassname(jsonObject.getString("classname"));
                list.add(cityBean);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }
}
